package com.sxds.wn.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;


public class CharactorFilter extends HttpServlet implements Filter {

	private FilterConfig filterConfig;
	private String charactorEncoding = "UTF-8";
	
	/**
	 * Handle the passed-in FilterConfig
	 */
	public void init(FilterConfig config) throws ServletException {
		this.filterConfig = config;
		String encoding = config.getInitParameter("encoding");
		if( encoding!=null ){
			this.charactorEncoding = encoding;
		}
	}

	/* Process the request/response pair*/
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		if (this.charactorEncoding != null ) {
			request.setCharacterEncoding(charactorEncoding);
		}
		filterChain.doFilter(request, response);
	}
	//Clean up resources
    public void destroy() {
    	this.charactorEncoding = null;
    	this.filterConfig = null;
    }
}
